package com.example.notuikitimdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.imsdk.TIMGroupAddOpt;
import com.tencent.imsdk.TIMGroupManager;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.TIMTextElem;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.session.SessionWrapper;
import com.tencent.imsdk.v2.V2TIMAdvancedMsgListener;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMCreateGroupMemberInfo;
import com.tencent.imsdk.v2.V2TIMGroupInfo;
import com.tencent.imsdk.v2.V2TIMGroupListener;
import com.tencent.imsdk.v2.V2TIMGroupMemberInfo;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMMessageManager;
import com.tencent.imsdk.v2.V2TIMOfflinePushInfo;
import com.tencent.imsdk.v2.V2TIMSDKConfig;
import com.tencent.imsdk.v2.V2TIMSDKListener;
import com.tencent.imsdk.v2.V2TIMSendCallback;
import com.tencent.imsdk.v2.V2TIMSimpleMsgListener;
import com.tencent.imsdk.v2.V2TIMValueCallback;

import java.util.ArrayList;
import java.util.List;

import static com.tencent.imsdk.v2.V2TIMGroupInfo.V2TIM_GROUP_ADD_ANY;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_input;
    private Button btn_send;
    private TextView tv_print;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        et_input = findViewById(R.id.et_input);
        btn_send = findViewById(R.id.btn_send);
        btn_send.setOnClickListener(this);
        tv_print = findViewById(R.id.tv_print);
        findViewById(R.id.btn_create_group).setOnClickListener(this);
        findViewById(R.id.btn_delete_group).setOnClickListener(this);
        findViewById(R.id.btn_add_group).setOnClickListener(this);
        findViewById(R.id.btn_exit_group).setOnClickListener(this);

        initIM();
        login();
        addMessageListener();
    }

    private void initIM() {
        if (SessionWrapper.isMainProcess(this)) {
            // 1. 从 IM 控制台获取应用 SDKAppID，详情请参考 SDKAppID。
            // 2. 初始化 config 对象
            V2TIMSDKConfig config = new V2TIMSDKConfig();
            // 3. 指定 log 输出级别，详情请参考 SDKConfig。
            config.setLogLevel(V2TIMSDKConfig.V2TIM_LOG_INFO);
            // 4. 初始化 SDK 并设置 V2TIMSDKListener 的监听对象。
            // initSDK 后 SDK 会自动连接网络，网络连接状态可以在 V2TIMSDKListener 回调里面监听。
            V2TIMManager.getInstance().initSDK(this, GenerateTestUserSig.SDKAPPID, config, new V2TIMSDKListener() {
                // 5. 监听 V2TIMSDKListener 回调
                @Override
                public void onConnecting() {
                    // 正在连接到腾讯云服务器
                    Log.i("yanze", "正在连接到腾讯云服务器");
                }

                @Override
                public void onConnectSuccess() {
                    // 已经成功连接到腾讯云服务器
                    Log.i("yanze", "已经成功连接到腾讯云服务器");
                }

                @Override
                public void onConnectFailed(int code, String error) {
                    // 连接腾讯云服务器失败
                    Log.i("yanze", "连接腾讯云服务器失败");
                }
            });
        }
    }

    private void login() {
        V2TIMManager.getInstance().login(Constants.userid, GenerateTestUserSig.genTestUserSig(Constants.userid), new V2TIMCallback() {
            @Override
            public void onError(int i, String s) {
                Log.i("yanze", "im登录失败" + i + " " + s);
            }

            @Override
            public void onSuccess() {
                Log.i("yanze", "im登录成功！");
            }
        });
    }

    //发送消息
    private void sendMessage() {
        String text = et_input.getText().toString();
        V2TIMManager.getInstance().sendGroupTextMessage(text, Constants.roomid, V2TIMMessage.V2TIM_PRIORITY_NORMAL, new V2TIMSendCallback<V2TIMMessage>() {
            @Override
            public void onProgress(int i) {
            }

            @Override
            public void onError(int i, String s) {
                Log.i("yanze", "消息发送失败！~" + i + " " + s);
            }

            @Override
            public void onSuccess(V2TIMMessage v2TIMMessage) {
                Log.i("yanze", "消息发送成功~");
            }
        });
    }

    //添加消息监听
    public void addMessageListener() {
        V2TIMManager.getInstance().addSimpleMsgListener(new V2TIMSimpleMsgListener() {
            @Override
            public void onRecvGroupTextMessage(String msgID, String groupID, V2TIMGroupMemberInfo sender, String text) {
                super.onRecvGroupTextMessage(msgID, groupID, sender, text);
                Log.i("yanze", "收到群组消息: " + sender.getUserID() + "/" + sender.getNickName() + ":" + text);
            }
        });
    }

    //创建群组
    private void createRoom() {
        // 示例代码：使用高级版 createGroup 创建一个工作群
        V2TIMGroupInfo v2TIMGroupInfo = new V2TIMGroupInfo();
        v2TIMGroupInfo.setGroupID(Constants.roomid);
        v2TIMGroupInfo.setGroupName("白泽直播间");
        v2TIMGroupInfo.setGroupType("Public");
        v2TIMGroupInfo.setIntroduction("this is a test Work group");
        v2TIMGroupInfo.setGroupAddOpt(V2TIMGroupInfo.V2TIM_GROUP_ADD_ANY);
        V2TIMManager.getGroupManager().createGroup(
                v2TIMGroupInfo, null, new V2TIMValueCallback<String>() {
                    @Override
                    public void onError(int code, String desc) {
                        // 创建失败
                        Log.i("yanze", "建群失败！~" + code + " " + desc);
                    }

                    @Override
                    public void onSuccess(String groupID) {
                        // 创建成功
                        Log.i("yanze", "建群成功！~");
                    }
                });
        //群消息监听
        V2TIMManager.getInstance().setGroupListener(new V2TIMGroupListener() {
            @Override
            public void onMemberEnter(String groupID, List<V2TIMGroupMemberInfo> memberList) {
                super.onMemberEnter(groupID, memberList);
                Log.i("yanze", memberList.get(0).getUserID() + "加入群聊~");
                Toast.makeText(HomeActivity.this, memberList.get(0).getUserID() + "加入群聊~", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onMemberLeave(String groupID, V2TIMGroupMemberInfo member) {
                super.onMemberLeave(groupID, member);
                Log.i("yanze", member.getUserID() + "离开群聊~");
                Toast.makeText(HomeActivity.this, member.getUserID() + "离开群聊~", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //解散群组
    private void deleteGroup() {
        V2TIMManager.getInstance().dismissGroup(Constants.roomid, new V2TIMCallback() {
            @Override
            public void onError(int i, String s) {
                Log.i("yanze", "解散失败！~" + i + " " + s);
            }

            @Override
            public void onSuccess() {
                Log.i("yanze", "解散成功！~");
            }
        });
    }

    //加入群组
    private void addGroup() {
        V2TIMManager.getInstance().joinGroup(Constants.roomid, "我加群", new V2TIMCallback() {
            @Override
            public void onError(int i, String s) {
                Log.i("yanze", "加群失败！~" + i + " " + s);
            }

            @Override
            public void onSuccess() {
                Log.i("yanze", "加群成功！~");
            }
        });
    }

    //退出群组
    private void exitGroup() {
        V2TIMManager.getInstance().quitGroup(Constants.roomid, new V2TIMCallback() {
            @Override
            public void onError(int i, String s) {
                Log.i("yanze", "推出失败！~" + i + " " + s);
            }

            @Override
            public void onSuccess() {
                Log.i("yanze", "推出成功！~");
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                sendMessage();
                break;
            case R.id.btn_create_group:
                Toast.makeText(this, "创建群组", Toast.LENGTH_SHORT).show();
                createRoom();
                break;
            case R.id.btn_delete_group:
                deleteGroup();
                break;
            case R.id.btn_add_group:
                addGroup();
                break;
            case R.id.btn_exit_group:
                exitGroup();
                break;
        }
    }
}
